<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工名称" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          placeholder="请输入员工名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="考勤日期" prop="attendanceDate">
        <el-date-picker clearable
          v-model="queryParams.attendanceDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择考勤日期">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes/rl:kqgl:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes/rl:kqgl:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes/rl:kqgl:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes/rl:kqgl:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="kqglList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="考勤ID" align="center" prop="attendanceId" />-->
      <el-table-column label="员工名称" align="center" prop="employeeName" />
<!--      <el-table-column label="考勤日期" align="center" prop="attendanceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.attendanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打卡时间(上班)" align="center" prop="clockInTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.clockInTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打卡时间(下班)" align="center" prop="clockOutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.clockOutTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="出勤天数" align="center" prop="workDuration" width="180">
      </el-table-column>
      <el-table-column label="迟到次数" align="center" prop="lateCount" />
      <el-table-column label="早退次数" align="center" prop="leaveEarlyCount" />
      <el-table-column label="缺勤" align="center" prop="absence" />
      <el-table-column label="加班时长" align="center" prop="overtimeDuration" width="180">

<!--        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.overtimeDuration, '{y}-{m}-{d}') }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="请假时长" align="center" prop="leaveDuration" width="180">
<!--        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveDuration, '{y}-{m}-{d}') }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="请假类型" align="center" prop="leaveType" />
      <el-table-column label="请假原因" align="center" prop="leaveReason" />
<!--      <el-table-column label="加班状态" align="center" prop="overtimeStatus" />-->
      <el-table-column label="考勤状态" align="center" prop="attendanceStatus" />
      <el-table-column label="考勤备注" align="center" prop="attendanceNotes" />
      <el-table-column label="异常说明" align="center" prop="exceptionExplain" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes/rl:kqgl:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes/rl:kqgl:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员考勤管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工名称" prop="employeeName">
          <el-input v-model="form.employeeName" placeholder="请输入员工名称" />
        </el-form-item>
<!--        <el-form-item label="考勤日期" prop="attendanceDate">
          <el-date-picker clearable
            v-model="form.attendanceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择考勤日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打卡时间" prop="clockInTime">
          <el-date-picker clearable
            v-model="form.clockInTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择打卡时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打卡时间" prop="clockOutTime">
          <el-date-picker clearable
            v-model="form.clockOutTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择打卡时间">
          </el-date-picker>
        </el-form-item>-->
        <el-form-item label="出勤天数" prop="workDuration">
          <el-input v-model="form.workDuration" placeholder="出勤天数" />
        </el-form-item>
        <el-form-item label="迟到次数" prop="lateCount">
          <el-input v-model="form.lateCount" placeholder="请输入迟到次数" />
        </el-form-item>
        <el-form-item label="早退次数" prop="leaveEarlyCount">
          <el-input v-model="form.leaveEarlyCount" placeholder="请输入早退次数" />
        </el-form-item>
        <el-form-item label="缺勤" prop="absence">
          <el-input v-model="form.absence" placeholder="请输入缺勤" />
        </el-form-item>
        <el-form-item label="加班时长" prop="overtimeDuration">

          <el-input v-model="form.overtimeDuration" placeholder="加班时长" />
        </el-form-item>
        <el-form-item label="请假时长" prop="leaveDuration">
          <el-input v-model="form.leaveDuration" placeholder="请假时长" />

        </el-form-item>
        <el-form-item label="请假类型" prop="leaveType">
          <el-input v-model="form.leaveType" placeholder="请假类型" />
<!--          <el-select v-model="form.leaveType" placeholder="请选择请假类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>-->
        </el-form-item>
        <el-form-item label="请假原因" prop="leaveReason">
          <el-input v-model="form.leaveReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="加班时长" prop="overtimeStatus">
          <el-input v-model="form.overtimeStatus" placeholder="加班时长" />
        </el-form-item>-->
        <el-form-item label="考勤状态" prop="attendanceStatus">
          <el-input v-model="form.attendanceStatus" placeholder="请输入考勤状态" />
        </el-form-item>
        <el-form-item label="考勤备注" prop="attendanceNotes">
          <el-input v-model="form.attendanceNotes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="异常说明" prop="exceptionExplain">
          <el-input v-model="form.exceptionExplain" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKqgl, getKqgl, delKqgl, addKqgl, updateKqgl } from "@/api/mes/rl/kqgl";

export default {
  name: "Kqgl",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员考勤管理表格数据
      kqglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeName: null,        attendanceDate: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        employeeName: [
          { required: true, message: "员工名称不能为空", trigger: "blur" }
        ],         }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人员考勤管理列表 */
    getList() {
      this.loading = true;
      listKqgl(this.queryParams).then(response => {
        this.kqglList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attendanceId: null,        employeeId: null,        employeeName: null,        attendanceDate: null,        clockInTime: null,        clockOutTime: null,        workDuration: null,        lateCount: null,        leaveEarlyCount: null,        absence: null,        overtimeDuration: null,        leaveDuration: null,        leaveType: null,        leaveReason: null,        overtimeStatus: null,        attendanceStatus: null,        attendanceNotes: null,        exceptionExplain: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attendanceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员考勤管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attendanceId = row.attendanceId || this.ids
      getKqgl(attendanceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员考勤管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.attendanceId != null) {
            updateKqgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKqgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const attendanceIds = row.attendanceId || this.ids;
      this.$modal.confirm('是否确认删除人员考勤管理编号为"' + attendanceIds + '"的数据项？').then(function() {
        return delKqgl(attendanceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/rl/kqgl/export', {
        ...this.queryParams
      }, `kqgl_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
